/*
package com.liuyu.common.framework.drools;

import org.drools.core.impl.InternalKnowledgeBase;
import org.drools.core.impl.KnowledgeBaseFactory;
import org.drools.template.ObjectDataCompiler;
import org.kie.api.KieBase;
import org.kie.api.io.Resource;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieSession;
import org.kie.internal.builder.KnowledgeBuilder;
import org.kie.internal.builder.KnowledgeBuilderFactory;
import org.kie.internal.io.ResourceFactory;
import org.kie.internal.utils.KieHelper;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

*/
/**
 * 1
 *
 * @author: liuyu
 * @date: 2021/11/2 16:41
 * @version:
 *//*

public class DroolsTests {
    public static void main(String[] args) throws Exception {

        KieHelper helper = new KieHelper();

// 先加载规则mytest>>11111

        helper.addContent(getRuleByOrg(11111, "mytest"), ResourceType.DRL);

        KieBase kieBase = helper.build();

        KieSession session = kieBase.newKieSession();

// 触发规则mytest>>11111

        System.out.println("触发规则：mytest>>11111");

        Message msg = new Message();

        msg.setOrgId(11111L);

        msg.setOrgKey(String.valueOf("k" + 11111));

        msg.setProductKey("cu3im1kts3rz8dSc");

        msg.setProductLabels("101,102,103");

        msg.setDeviceLabels("201,202,203");

        session.insert(msg);

        session.fireAllRules();

// 移除规则mytest>>11111

        System.out.println("删除规则：mytest>>11111");

        kieBase.removeRule("drools.example", "mytest");

// 重新添加规则mytest>>22222

        System.out.println("载入新规则：mytest>>22222");

        KnowledgeBuilder kb = KnowledgeBuilderFactory.newKnowledgeBuilder();

// 装入规则，可以装入多个

// kb.add(ResourceFactory.newByteArrayResource(drlStr2.getBytes("utf-8")),

// ResourceType.DRL);

        kb.add(ResourceFactory.newByteArrayResource(getRuleByOrg(22222, "mytest").getBytes("utf-8")), ResourceType.DRL);

// 重用session方式

        ((InternalKnowledgeBase) kieBase).addPackages(kb.getKnowledgePackages());

        System.out.println("重用session方式，触发规则：mytest>>22222");

        msg = new Message();

        msg.setOrgId(22222L);

        msg.setOrgKey(String.valueOf("k" + 22222));

        msg.setProductKey("cu3im1kts3rz8dSc");

        msg.setProductLabels("101,102,103");

        msg.setDeviceLabels("201,202,203");

        session.insert(msg);

        session.fireAllRules();

        session.dispose();

// 新开session

        InternalKnowledgeBase ikBase = KnowledgeBaseFactory.newKnowledgeBase();

        ikBase.addPackages(kb.getKnowledgePackages());

        session = ikBase.newKieSession();

        msg = new Message();

        msg.setOrgId(22222L);

        msg.setOrgKey(String.valueOf("k" + 22222));

        msg.setProductKey("cu3im1kts3rz8dSc");

        msg.setProductLabels("101,102,103");

        msg.setDeviceLabels("201,202,203");

        session.insert(msg);

        System.out.println("重新创建session方式，触发规则：mytest>>22222");

        session.fireAllRules();

    }


    public static String getRuleByOrg(long orgId, String ruleName) {

        ObjectDataCompiler odc = new ObjectDataCompiler();

        try {

//执行与管理以http接口通信

            Resource r1 = ResourceFactory.newUrlResource(new URL("http://127.0.0.1:8080/rule/getForwardRules?orgId=" + orgId + "&ruleName=" + ruleName));

            Map<String, Object> data = new HashMap<>();
//账户信息
            data.put("orgId", orgId);
            data.put("orgKey", String.valueOf("k" + orgId));
            data.put("orgName", "xxyyzz");
//设备产品信息
            data.put("productKey", "cu3im1kts3rz8dSc");
            data.put("productName", "智慧水表");
            data.put("deviceKey", "QoearAEbXaU1meN");
//产品设备标签
            data.put("productLabels", "101,102,103");
            data.put("deviceLabels", "201,202,203");
//其它信息
            data.put("type", "123");
            data.put("action", 1);
            return odc.compile(Arrays.asList(data), r1.getInputStream());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;

    }

}
*/
